package cn.lixin.hrm.filter;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SmsCheakCodeFilter extends OncePerRequestFilter {

    /**
     * =====  校验参数  =====
     *
     * Same contract as for {@code doFilter}, but guaranteed to be
     * just invoked once per request within a single request thread.
     * See {@link #shouldNotFilterAsyncDispatch()} for details.
     * <p>Provides HttpServletRequest and HttpServletResponse arguments instead of the
     * default ServletRequest and ServletResponse ones.
     *
     * @param request
     * @param response
     * @param filterChain
     */
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        // check uri
        String uri = request.getRequestURI();
        if(uri.equals("/smslogin")){
            // 校验参数
            String phone = request.getParameter("phone");
            String code = request.getParameter("code");
            if (StringUtils.isBlank(phone) || StringUtils.isBlank(code)){
                throw new RuntimeException("手机或验证码为空");
            }
        }
        // 不是/sms/send请求或者code正确就  放行
        filterChain.doFilter(request,response);
    }
}
